package com.baomidou.springboot.controller.manager;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springboot.entity.News;
import com.baomidou.springboot.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Controller("ManagerNewsControler")
@RequestMapping("/manager/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(HttpServletRequest request, Page<News> page, String search) {
        request.setAttribute("search", search);
        request.setAttribute("page", newsService.list(page, search));
        return "manager/news/list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return "manager/news/add";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(News news) {
        newsService.save(news);
        return "redirect:list";
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String view(HttpServletRequest request, String id) {
        request.setAttribute("news", newsService.view(id));
        return "manager/news/view";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, String id) {
        request.setAttribute("news", newsService.edit(id));
        return "manager/news/edit";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(News news) {
        newsService.update(news);
        return "redirect:list";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(String id) {
        newsService.delete(id);
        return "redirect:list";
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    public String deleteBatch(String[] ids) {
        if (null != ids && ids.length > 0) {
            newsService.deleteBatch(Arrays.asList(ids));
        }
        return "redirect:list";
    }
}
